<?php
/**
 * 
 * @author wb_wenjie.wuwj <wb_wenjie.wuwj@alibaba-inc.com>
 * @copyright 2013-3-28 yahoo
 */

class Manager extends CActiveRecord
{
	const SECRET_KEY = '52c30HNT';
	public $passwordConfirm;
	public $passwordOrigin;
	public static function model($className = __CLASS__)
	{
		return parent::model($className);
	}
	
	
	public function rules()
	{
		return array(
				array('loginname, remark, password', 'filter', 'filter'=>'trim'),
				array('loginname, password', 'required'),
				array('loginname', 'length', 'min'=>5, 'max'=>20),
				array('loginname', 'unique', 'className'=>'Manager'),
				array('password, passwordOrigin', 'length', 'min'=>6, 'max'=> 20),
				array('passwordOrigin', 'authenticate', 'on'=>'update'),
				array('passwordConfirm', 'compare', 'compareAttribute'=>'password', 'on'=>'create,update'),
				array('remark', 'length', 'max'=>30),
		);
	}
	
	public function tableName()
	{
		return '{{manager}}';
	}
	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
				'id' => 'ID',
				'loginname' => '登录名',
				'password' => '密码',
				'logintimes' => 'Logintimes',
				'loginip' => 'Loginip',
				'lastlogintime' => 'Lastlogintime',
				'logintime' => 'Logintime',
				'islocked' => 'Islocked',
				'remark' => 'Remark',
		);
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
	
		$criteria=new CDbCriteria;
	
		$criteria->compare('id',$this->id,true);
		$criteria->compare('loginname',$this->loginname,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('logintimes',$this->logintimes,true);
		$criteria->compare('loginip',$this->loginip,true);
		$criteria->compare('lastlogintime',$this->lastlogintime,true);
		$criteria->compare('logintime',$this->logintime,true);
		$criteria->compare('islocked',$this->islocked);
		$criteria->compare('remark',$this->remark,true);
	
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}
	
	public function generatePassword($password = NULL)
	{
		$password = is_null($password) ? $this->getAttribute('password') : $password;
		return md5($password.self::SECRET_KEY);
		
	}
	
	public function authenticateLoginname($attribute, $params)
	{
		$manager = $this->find('LOWER(loginname)=?',array(strtolower($this->getAttribute($attribute))));
		if ($manager !== NULL)
		{
			$this->addError('loginname', 'Loginname is exist!');
		}
	}
	
	public function authenticate($attribute, $params)
	{
		$loginname = strtolower($this->getAttribute('loginname'));
		$password = $this->generatePassword($this->getAttribute($attribute));
		$manager = $this->find('LOWER(loginname)=? and password=?',array($loginname,$password));
		if ($manager === NULL)
		{
			$this->addError('passwordOrigin','Input passwordOrigin.');
		}
	}
	
	public function beforeSave()
	{
		$this->setAttribute('password', $this->generatePassword());
		$this->setAttribute('loginname', strtolower($this->getAttribute('loginname')));
		if (!$this->isNewRecord)
		{
			$this->setAttribute('logintimes', $this->getAttribute('logintimes')+1);
		}
		$this->setAttribute('loginip', '');
		return parent::beforeSave();
	}
	
}